package com.sqoq.downloader3

import android.app.Application
import android.content.Context
import android.content.res.Resources
import com.hjq.language.MultiLanguages
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.sqoq.downloader3.lanuage_ddg.Language_ddg
import com.sqoq.downloader3.retrofit.ApiService
import com.sqoq.downloader3.retrofit.HeaderGetter
import com.sqoq.downloader3.retrofit.RetrofitFactory
import com.tencent.mmkv.MMKV
import xyz.doikki.videoplayer.exo.ExoMediaPlayerFactory
import xyz.doikki.videoplayer.player.AndroidMediaPlayerFactory
import xyz.doikki.videoplayer.player.VideoViewConfig
import xyz.doikki.videoplayer.player.VideoViewManager

class App : Application() {
    override fun onCreate() {
        super.onCreate()

        MMKV.initialize(this)
        MMKV.defaultMMKV(MMKV.MULTI_PROCESS_MODE, null)

        MultiLanguages.init(this)
        app = this
        apiService = initHTTP()
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout -> //指定为经典Footer，默认是 BallPulseFooter
            ClassicsFooter(context).setDrawableSize(20f).setTextSizeTitle(0f)
        }
        VideoViewManager.setConfig(
            VideoViewConfig.newBuilder()
            //使用使用IjkPlayer解码
//            .setPlayerFactory(IjkPlayerFactory.create())
            //使用ExoPlayer解码
            .setPlayerFactory(ExoMediaPlayerFactory.create())
            //使用MediaPlayer解码
            .setPlayerFactory(AndroidMediaPlayerFactory.create())
            .build());

    }

    override fun attachBaseContext(base: Context?) {
        // 绑定语种
        super.attachBaseContext(MultiLanguages.attach(base))
    }

    companion object {
        lateinit var apiService: ApiService
        lateinit var app: App
        val PrivacyPolicy_ddg= "https://sites.google.com/view/privacy-policy-for-quicksave-h/"

        val TermofUser_ddg="https://sites.google.com/view/terms-of-service-for-quicksave/"

    }

    private fun initHTTP(): ApiService {
        val retrofitFactory = RetrofitFactory()
        return retrofitFactory.init(
            ApiService::class.java,
            "http://35.73.28.223/",
            object : HeaderGetter() {
                override fun getHeader(): HashMap<String, String> {
                    return HashMap<String, String>()
                }
            })
    }
}

fun Resources.getLanguageString_ddg(key: String): String {
    var a = 0
    var text = ""
    when (a) {
        0 -> text = Language_ddg.getLanguageString(key)
//        3 -> text = Language_ddg_zh.getLanguageString(key)
//        2 -> text = Language_ddg_es.getLanguageString(key)
//        1 -> text = Language_ddg_fr.getLanguageString(key)
//        6 -> text = Language_ddg_ja.getLanguageString(key)
//        7 -> text = Language_ddg_ko.getLanguageString(key)
//        4 -> text = Language_ddg_pt.getLanguageString(key)
        else -> ""
    }
    if (text.isNullOrEmpty()) {
        text = Language_ddg.getLanguageString(key)
    }
    return text

}

fun Context.getLanguageString_ddg(key: String): String {
    return resources.getLanguageString_ddg(key = key)
}

